<template>
    <div class="login_continer">

        <div class="login_box">
            <!--头像区域-->
            <div class="login_headerimg">
                <img src="../assets/logo.png" alt="">
            </div>
            <!--登录表单区域-->
            <el-form :model="loginform" ref="loginFormRef" :rules="loginrules" label-width="0px" class="login_form">
                <!--用户名-->
                <el-form-item prop="username">
                    <el-input v-model="loginform.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <!--密码-->
                <el-form-item  prop="password">
                    <el-input v-model="loginform.password" prefix-icon="iconfont icon-3702mima" type="password" placeholder="请输入登录密码"></el-input>
                </el-form-item>
                <!--按钮-->
                <el-form-item class="btns">
                    <el-button @click="onSubmit" type="primary">登录</el-button>
                    <el-button @click="resetbtn" type="info">重置</el-button>
                </el-form-item>

            </el-form>
        </div>

        <!--<router-view/>-->
        <router-view></router-view>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return {
                loginform: {
                    username:'admin',
                    password:'123456'
                },
                loginrules:{
                    username: [
                        { required: true, message: '请输入用户登录名', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password:[
                        { required: true, message: '请输入登录密码', trigger: 'blur' },
                        { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods :{
            onSubmit(){
                this.$refs.loginFormRef.validate(async valid =>{
                    //valid本地校验数据
                    if (!valid) return;
                    //登录post请求
                    const {data:res} =await this.$http.post("login",this.loginform);
                    // console.log(res);
                    if (res.meta.status != 200) return this.$message.error(res.meta.msg) ;
                    this.$message.success('登录成功');
                //    保存token到sessionStirage
                //    利用编程式导航，路由跳转到home
                    window.sessionStorage.setItem('token',res.data.token);
                    this.$router.push('/home');
                })
            },
            resetbtn(){
                //resetFields 重置  $refs.loginFormRef可以通过打印this $看到refs返回对象中loginFormRef
                this.$refs.loginFormRef.resetFields();
                console.log(this);
            }
        }
    }
</script>

<style lang="less" scoped>
    .login_continer {
        height: 100%;
        background-color: #2b4b6b;
    }

    .login_box {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        transform:translate(-50%,-50%);

        .login_headerimg {
            position: absolute;
            left: 50%;
            width: 130px;
            height: 130px;
            border-radius: 50%;
            border: 1px solid #eee;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            transform: translate(-50%,-50%);
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
    }

    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }

    .btns {
        display: flex;
        justify-content: space-between;
        /*justify-content: flex-end; 尾部显示*/
    }


</style>